<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
		function getPosting() {
			$db = new Connect();
			
			$sql = "SELECT mi.mID, c.cName, p.pName, p.status, p.img, p.pDesc, mi.mName, mi.email, mi.mCity, IFNULL(mp.mName, 1) AS showName, IFNULL(mp.email, 1) AS showEmail, IFNULL(mp.mCity, 1) AS showCity " .
				   "FROM Posting p " .
			       "INNER JOIN Category c ON c.cID = p.cID " .
			       "INNER JOIN MemberInfo mi ON mi.mID = p.mID " .
			       "LEFT JOIN MemberPrivacy mp ON mp.mID = mi.mID " .
			       "WHERE p.pID = " . $_GET['pid'];
				   
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find posting
			{
				return;
			}
			else // Return posting info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}
    ?>
    <div id="main">
		<p class="title">
			Posting
		</p>
<?php
	$posting = getPosting();
	if (isset($posting))
	{
		print "<p class=\"normal\">Category: " . $posting['cName'] . "</p>";
		print "<p>";
		print "<table border=\"0\">";
		print "<col width=\"0\">";
		print "<col width=\"300px\">";
		print "<tr>";
		print "<td class=\"normal\">";
		print $posting['pName'] . "<br/>";
		print "<img src=\"" . $posting['img'] . "\" height=\"100px\" width=\"100px\" alt=\"Image\"/><br/>";
		print "<input type=\"button\" class=\"smallButton\" value=\"Report posting\" onclick=\"parent.location='reportpost.php?pid=" . $_GET['pid'] . "'\"/>";
		print "</td>";
		print "<td class=\"normal-right\">";
		if ($posting['showName'] && isLoggedIn())
			print $posting['mName'] . "<br/>";
		if ($posting['showEmail'] && isLoggedIn())
			print $posting['email'] . "<br/>";
		if ($posting['showCity'] && isLoggedIn())
			print $posting['mCity'] . "<br/>";
		print "</td>";
		print "</tr>";
		print "</table>";
		print "</p>";
		print "<p class=\"normal\">" . $posting['pDesc'] . "</p>";
		if (($posting['status'] == "Open" && isLoggedIn()) || ($posting['status'] == "Closed" && isLoggedIn() && $posting['mID'] == $_SESSION['mid']))
			print " <input type=\"button\" class=\"largeButton\" value=\"Bid\" onclick=\"parent.location='bid.php?pid=" . $_GET['pid'] . "'\"/>";
		if (($posting['status'] == "Barter" && isLoggedIn()) || ($posting['status'] == "Traded" && isLoggedIn() && $posting['mID'] == $_SESSION['mid']))
			print " <input type=\"button\" class=\"largeButton\" value=\"Barter\" onclick=\"parent.location='bid.php?pid=" . $_GET['pid'] . "'\"/>";
		if ($posting['status'] == "Garage" || $posting['status'] == "Reserved")
			print " <input type=\"button\" class=\"largeButton\" value=\"Reserve\" onclick=\"parent.location='reservepost.php?pid=" . $_GET['pid'] . "'\"/>";
		if (isAdmin() || $posting['mID'] == getMemberID())
			print " <input type=\"button\" class=\"largeButton\" value=\"Edit\" onclick=\"parent.location='editpost.php?pid=" . $_GET['pid'] . "'\"/>";
		if (isAdmin())
			print " <input type=\"button\" class=\"largeButton\" value=\"Remove\" onclick=\"parent.location='removepost.php?pid=" . $_GET['pid'] . "'\"/>";
	}
	else
	{
		print "<p></p><span class=\"error\">INVALID POSTING SPECIFIED!</span>";
	}
?>
    </div>
</body>
</html>
